package com.zhong.auth.application.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.zhong.auth.application.convert.AuthUserDTOConvert;
import com.zhong.auth.application.dto.AuthUserDTO;
import com.zhong.auth.common.entity.Result;
import com.zhong.auth.domain.bo.AuthUserBO;
import com.zhong.auth.domain.service.AuthUserDomainService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 用户controller
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class AuthUserController {

    @Resource
    AuthUserDomainService authUserDomainService;

    /**
     * 获取用户信息
     */
    @GetMapping("/getUserInfo")
    public Result<AuthUserDTO> getUserInfo(@RequestBody AuthUserDTO authUserDTO){
        try {
            if (log.isInfoEnabled()){
                log.info("AuthUserController.getUserInfo.authUserDTO: {}",authUserDTO);
            }
            //数据校验
            Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getUserName()),"用户账号不能为空！！！");

            AuthUserBO authUserBO = AuthUserDTOConvert.INSTANCE.convertDTOToBO(authUserDTO);
            AuthUserBO authUserBOResult = authUserDomainService.getUserInfo(authUserBO);
            AuthUserDTO authUserDTOResult = AuthUserDTOConvert.INSTANCE.convertBOToDTO(authUserBOResult);
            return Result.ok(authUserDTOResult);
        }catch (Exception e){
            log.error("AuthUserController.getUserInfo.error: {}",e.getMessage(),e);
            return Result.fail("查询失败");
        }
    }
    /**
     * 用户登入
     */
    @PostMapping("/doLogin")
    public Result<SaTokenInfo> doLogin(@RequestParam("validCode") String validCode){
        try {
            if (log.isInfoEnabled()){
                log.info("AuthUserController.doLogin.validCode: {}",validCode);
            }
            //数据校验
            Preconditions.checkArgument(!StringUtils.isBlank(validCode),"验证码不能为空！！！");
            Preconditions.checkArgument(validCode.length() == 6,"验证码不为6位！！！");

            SaTokenInfo saTokenInfo = authUserDomainService.doLogin(validCode);
            return Result.ok(saTokenInfo);
        }catch (Exception e){
            log.error("AuthUserController.doLogin.error: {}",e.getMessage(),e);
            return Result.fail("登入失败");
        }
    }

    /**
     * 判断用户是否登入成功
     * @return
     */
    @PostMapping("/isLogin")
    public Result<Boolean> isLogin(){
        return Result.ok(StpUtil.isLogin());
    }


    /**
     * 用户注册接口
     */
    @PostMapping("/register")
    public Result<Boolean> register(@RequestBody AuthUserDTO authUserDTO){
        try {
            if (log.isInfoEnabled()){
                log.info("AuthUserController.register.authUserDTO：{}", JSON.toJSONString(authUserDTO));
            }

            //数据校验
            Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getUserName()),"用户名不能为空！！！");
            Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getEmail()),"邮件地址不能为空！！！");
            Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getPassword()),"密码不能为空！！！");

            AuthUserBO authUserBO = AuthUserDTOConvert.INSTANCE.convertDTOToBO(authUserDTO);


            return Result.ok(authUserDomainService.register(authUserBO));
        }catch (Exception e){
            log.error("AuthUserController.register.error：{}", e.getMessage(),e);
            return Result.fail("注册用户失败");
        }
    }

    /**
     * 修改用户信息
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody AuthUserDTO authUserDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("AuthUserController.update.authUserDTO: {}",JSON.toJSONString(authUserDTO));
            }
            //数据校验
            Preconditions.checkNotNull(authUserDTO.getId(),"用户id不能为空");

            AuthUserBO authUserBO = AuthUserDTOConvert.INSTANCE.convertDTOToBO(authUserDTO);

            return Result.ok(authUserDomainService.update(authUserBO));
        }catch (Exception e){
            log.error("AuthUserController.update.error: {}",e.getMessage(),e);
            return Result.fail("修改用户信息失败");
        }
    }

    /**
     * 删除用户
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody AuthUserDTO authUserDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("AuthUserController.delete.authUserDTO: {}",JSON.toJSONString(authUserDTO));
            }
            //数据校验
            Preconditions.checkNotNull(authUserDTO.getId(),"用户id不能为空");

            AuthUserBO authUserBO = AuthUserDTOConvert.INSTANCE.convertDTOToBO(authUserDTO);

            return Result.ok(authUserDomainService.delete(authUserBO));
        }catch (Exception e){
            log.error("AuthUserController.delete.error: {}",e.getMessage(),e);
            return Result.fail("删除用户失败");
        }
    }

    /**
     * 用户的禁用和启用
     */
    @PostMapping("/changeStatus")
    public Result<Boolean> changeStatus(@RequestBody AuthUserDTO authUserDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("AuthUserController.update.authUserDTO: {}",JSON.toJSONString(authUserDTO));
            }
            //数据校验
            Preconditions.checkNotNull(authUserDTO.getId(),"用户id不能为空");
            Preconditions.checkNotNull(authUserDTO.getStatus(),"状态不能为空");

            AuthUserBO authUserBO = AuthUserDTOConvert.INSTANCE.convertDTOToBO(authUserDTO);

            return Result.ok(authUserDomainService.changeStatus(authUserBO));
        }catch (Exception e){
            log.error("AuthUserController.update.error: {}",e.getMessage(),e);
            return Result.fail("用户的禁用和启用失败");
        }
    }




}
